<template>
	<view class="container">
		<view class="title">{{title}}</view>
		<view class="nav-box">
			<view class="fs32">温馨提醒：</view>
			<view class="text-intent">请使用微信扫码，否则无法正常使用。</view>
		</view>

		options:{{options}}
		<!-- 
		<br />
		lineqrcode-res：{{lineqrcoderes}}
		<br />
		gettoken_res{{gettoken_res}}
		<br />
		gettokenrefresh_res{{gettokenrefresh_res}}
		<br />
		gettokenverify_res{{gettokenverify_res}} -->



		<view class="btn-box">
			<button type="primary" @click="goindex()">进入到首页</button>
		</view>
	</view>
</template>

<script>
	import {
		getorderqrcode,
		gettoken,
		gettokenrefresh,
		gettokenverify,
		getinstructor,
		getUserinfo
	} from '@/common/api/index.js'
	export default {
		data() {
			return {
				title: '扫码成功',
				query: {},
				ensure: true,
				options: {},
				lineqrcoderes: {},
				gettoken_res: {},
				gettokenrefresh_res: {},
				gettokenverify_res: {},
				channel:"wechatpay"
			}
		},
		onLoad(options) {
			// 判断是微信扫码进来的，还是支付宝扫码进来的  默认微信
		
			var ua = navigator.userAgent.toLowerCase();
			if (ua.match(/micromessenger/i)) {
				this.channel='wechatpay'
			} else if (ua.match(/alipayclient/i)) {
				this.channel='alipay'
			} else {
				this.channel='wechatpay'
			}
			console.log(this.channel, '扫码进来的方式');
			// 获取到了扫描方式，就存起来
			uni.setStorageSync('channel', this.channel)
			
			console.log(options)
			this.options = options
			// 拿到扫码的参数，也存起来
			if (options.openid) {uni.setStorageSync('openId', options.openid) } 
			if (options.imei1) {uni.setStorageSync('imei1', options.imei1) }
			if (options.androidID) {uni.setStorageSync('androidID', options.androidID) }
			if (options.serial) {uni.setStorageSync('serial', options.serial) }
			if (options.uuid) {uni.setStorageSync('uuid', options.uuid) }
			if (options.mac) {uni.setStorageSync('mac', options.mac) }
			if (options.deviceUnique) {uni.setStorageSync('deviceUnique', options.deviceUnique) }
			if (options.clazz) {uni.setStorageSync('clazz', options.clazz) }
			
			
			// 判断是否是师傅分享过来的
			let visible_o = options.visible;
			let commodity_id_o = options.commodity_id;
			let Coach_id_o = options.Coach_ID;
			if(visible_o){
				uni.setStorageSync('visible',visible_o);
			}
			if(commodity_id_o){
				uni.setStorageSync('commodity_id',commodity_id_o);
			}
			if(Coach_id_o){
				uni.setStorageSync('Coach_ID',Coach_id_o);
			}
			
			
			// this.getlineqrcodeData()
			
			// this.getInstructorList()
		},
		methods: {
			goindex() {
				wx.switchTab({
					url: '/pages/student/index/index', // 替换为你的 tabBar 页面路径
					success: function(res) {
						// 跳转成功的回调
						console.log(res, '跳转成功')
					},
					fail: function(err) {
						// 错误处理
						console.log(err, '为什么不跳转')
					}
				});
			},
			gettokenList() {
				gettoken({}).then(res => {
					this.gettoken_res = res
				})
				gettokenrefresh({}).then(res => {
					this.gettokenrefresh_res = res
				})
				gettokenverify({}).then(res => {
					this.gettokenverify_res = res
				})
			},
			getlineqrcodeData() {
				let params = {
					line_id: 1,//路线id
					franchisee_id: 1,//加盟商id或者总校id
					instructor_id: 1,//教练id
					clazz: 1,//套餐id
					commodity_id: 1,//商品id
					device_id: 1,//设备ID
					openid: this.options.openid,
					channel: this.channel
				} 
				getorderqrcode(params).then(res => {
					// 获取到数据后，判断是否购买状态，购买了的话，就调用接口开始计时，没有调用就跳转套餐页面
					console.log(res, '判断是否登录');
					this.lineqrcoderes = res
					// 跳转到首页
					if (res.status != 100005) { 
						// 跳转之前判断是学员，还是教练
						this.getInstructorList()
					} else {
						//提示没有登录
						uni.showToast({
							title: "登录过期，5秒后请重新登录",
							icon: 'none'
						})
						setTimeout(function() {
							wx.switchTab({
								url: '/', // 替换为你的 tabBar 页面路径
								success: function(res) {
									// 跳转成功的回调
									console.log(res, '跳转成功')
								},
								fail: function(err) {
									// 错误处理
									console.log(err, '为什么不跳转')
								}
							});
						}, 5000)
					}
				}).catch(res=>{
					//提示没有登录
					uni.showToast({
						title: "请重新登录",
						icon: 'none'
					})
					setTimeout(function() {
						wx.switchTab({
							url: '/', // 替换为你的 tabBar 页面路径
							success: function(res) {
								// 跳转成功的回调
								console.log(res, '跳转成功')
							},
							fail: function(err) {
								// 错误处理
								console.log(err, '为什么不跳转')
							}
						});
					}, 5000)
				})
			},
			// 判断是否是教练，跳转的页面不一样的
			getInstructorList(){
				// 获取用户信息，然后判读是不是教练
				getUserinfo().then(res=>{
					console.log(res,'resgetUserinfo');
					if(res.data){
						// 不是教练
						getinstructor({instructor_id:res.data.records[0].id}).then(res2=>{
							console.log(res2,'res');
							if(res2.status==0){
								// 是教练
								uni.setStorageSync('isCoach',1) 
							}else{
								// 不是教练
								uni.setStorageSync('isCoach',0)
							}
							// 跳转到首页，然后再进行判断
							wx.switchTab({
								url: '/pages/student/index/index', // 替换为你的 tabBar 页面路径
								success: function(res) {
									// 跳转成功的回调
									console.log(res, '跳转成功')
								},
								fail: function(err) {
									// 错误处理
									console.log(err, '为什么不跳转')
								}
							}); 
						})
					}
					
				})
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	.title {
		font-size: 42rpx;
		text-align: center;
		font-weight: bold;
		padding: 120rpx 0 40rpx;
	}

	.name {
		font-size: 62rpx;
		font-weight: bold;
		color: #ffffff;
		letter-spacing: 2rpx;

	}

	.img {
		width: 88vw;
		z-index: -1;
	}

	.nav-box {
		padding: 40rpx;
		color: #f10000;
		line-height: 50rpx;
	}

	.text-intent {
		text-indent: 60rpx;
	}
</style>